﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace FireAlarmService
{
    public class FireAlarmInfo
    {
        private string alarmType;
        private DateTime alarmTime;
        private string controlPanel;
        private string card;
        private string device;

        private string alarmString;

        public FireAlarmInfo(string alarmString)
        {
            this.AlarmString = alarmString;
        }

        /// <summary>
        /// 报警类型
        /// </summary>
        public string AlarmType
        {
            get
            {
                return alarmType;
            }

            set
            {
                alarmType = value;
            }
        }
        /// <summary>
        /// 报警时间
        /// </summary>
        public DateTime AlarmTime
        {
            get
            {
                return alarmTime;
            }

            set
            {
                alarmTime = value;
            }
        }
        /// <summary>
        /// 控制盘号
        /// </summary>
        public string ControlPanel
        {
            get
            {
                return controlPanel;
            }

            set
            {
                controlPanel = value;
            }
        }
        /// <summary>
        /// 卡号
        /// </summary>
        public string Card
        {
            get
            {
                return card;
            }

            set
            {
                card = value;
            }
        }
        /// <summary>
        /// 设备号
        /// </summary>
        public string Device
        {
            get
            {
                return device;
            }

            set
            {
                device = value;
            }
        }
        /// <summary>
        /// 待分析的报警字符串
        /// </summary>
        public string AlarmString
        {
            get
            {
                return alarmString;
            }

            set
            {
                alarmString = value;
                Analysis();
            }
        }

        private void Analysis()
        {
            if (!string.IsNullOrEmpty(AlarmString))
            {
                try
                {
                    string[] preresult = Regex.Split(AlarmString, "\r\n", RegexOptions.IgnoreCase);
                    string[] result = Regex.Split(preresult[1], " ", RegexOptions.IgnoreCase);
                    AlarmType = result[0];
                    AlarmTime = DateTime.ParseExact($"{result[4]} {result[5]}", "HH:mm:ss MM/dd/yyyy", System.Globalization.CultureInfo.CurrentCulture);
                    ControlPanel = result[6].Split(':')[1];
                    Card = result[8].Split(':')[1];
                    Device = result[10].Split(':')[1];
                }
                catch
                {
                    AlarmType = "错误";
                    AlarmTime = DateTime.ParseExact("00:00:00 01/01/1900", "HH:mm:ss MM/dd/yyyy", System.Globalization.CultureInfo.CurrentCulture);
                    ControlPanel = "00";
                    Card = "00";
                    Device = "0000";
                }
            }
        }
        public override string ToString()
        {
            return $"{ControlPanel} {Card} {Device} {AlarmTime.ToString()} {AlarmType}";
        }
    }
}
